package com.project.service.impl;

import com.project.bean.CarBean;
import com.project.bean.ManBean;
import com.project.mapper.IManMapper;
import com.project.service.IManService;
import org.apache.ibatis.session.SqlSession;

import java.sql.SQLClientInfoException;
import java.util.Arrays;
import java.util.List;

public class ManServiceImpl extends BaseService implements IManService {
    @Override
    public List<ManBean> findAll() {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        List<ManBean> list = mapper.findAll();
        session.close();
        return list;
    }

    @Override
    public void addMan(ManBean man, List<CarBean> carList) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        //完成公民添加
        mapper.addMan(man);
        //添加汽车集合，所有汽车外键都是新加公民的ID
        mapper.addCar(carList, man.getId());

        session.commit();
        session.close();
    }

    @Override
    public void delSetNull(int id) {
        SqlSession session = this.getSession();
        IManMapper mapper = session.getMapper(IManMapper.class);
        mapper.delSetNull(id);
        session.commit();
        session.close();
    }

    @Override
    public ManBean findByIdIncludeCar(int id) {
        SqlSession session = this.getSession();
        IManMapper mapper =  session.getMapper(IManMapper.class);
        ManBean man = mapper.findByIdIncludeCar(id);
        session.close();

        return man;
    }

    public static void main(String[] args) {
        IManService service = new ManServiceImpl();
//        System.out.println(service.findAll());
//        service.addMan(new ManBean("李江", "18920398473"),
//                Arrays.asList(new CarBean("大运",40000),
//                        new CarBean("法拉利", 1000000)));
//        service.delSetNull(5);
        System.out.println(service.findByIdIncludeCar(1));
    }
}
